from django.db import models

class City(models.Model):
    name = models.CharField(max_length=100, unique=True)  # 城市名称
    code = models.CharField(max_length=10, unique=True)  # 城市代码

    def __str__(self):
        return self.name

class DataCenter(models.Model):
    name = models.CharField(max_length=100)  # 机房名称
    city = models.ForeignKey(City, related_name='data_centers', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Host(models.Model):
    name = models.CharField(max_length=100)  # 主机名
    ip_address = models.GenericIPAddressField(unique=True)  # IP地址
    data_center = models.ForeignKey(DataCenter, related_name='hosts', on_delete=models.CASCADE)
    is_online = models.BooleanField(default=True)  # 是否在线，默认在线
    created_at = models.DateTimeField(auto_now_add=True)  # 添加时间
    updated_at = models.DateTimeField(auto_now=True)  # 修改时间

    def __str__(self):
        return self.name

class HostStatistics(models.Model):
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    data_center = models.ForeignKey(DataCenter, on_delete=models.CASCADE)
    host_count = models.PositiveIntegerField()  # 主机数量
    date = models.DateField()  # 统计日期

    class Meta:
        unique_together = ('city', 'data_center', 'date')

    def __str__(self):
        return f"Statistics for {self.city.name} - {self.data_center.name} on {self.date}"
